﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace YXShop.Web.admin.accessories
{
    public partial class question_log : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {   
            if(!IsPostBack)
            {
                YXShop.Common.PromptInfo.Popedom("012002006");
                string strID = Request.QueryString["ID"];
                YXShop.BLL.Accessories.Question questBll = new YXShop.BLL.Accessories.Question();
                string strwt = questBll.GetModelById(Convert.ToInt32(strID)).QuestionDiscription;
                if (Request.QueryString["QuestType"] == "1" || Request.QueryString["QuestType"] == "2")
                {
                    string strwt1 = questBll.GetModelById(Convert.ToInt32(strID)).QuestionDiscription;
                    Get(strwt, strwt1);

                }
                else
                {
                    string[] arrContent = strwt.Split('|');
                    decimal all = 0;
                    foreach (string strContentIN in arrContent)
                    {
                        all = Convert.ToDecimal(strContentIN.Split(',')[1]) + all;
                    }

                    GetResult(strwt, all);
                }
            }
            
        }



        #region 成员方法

        public void GetResult(string strContent, decimal decNumAll)
        {
            string[] arrContent = strContent.Split('|');
            string strBody = "";

            foreach (string strContentIN in arrContent)
            {
                string strItemName = strContentIN.Split(',')[0].ToString();//得到选项名称
                decimal decItemNum = Convert.ToDecimal(strContentIN.Split(',')[1]);//得到选项的投票数
                decimal decPercent = GetPercent(decItemNum, decNumAll) * 100;//得到百分比 
                string strPercent = decPercent.ToString();//将百分比转为字符型
                if (strPercent.Length > 5)//如果百分比结果长度超过5位
                {
                    strPercent = strPercent.Substring(0, 5);//将百分比的余数截短为"00.00"
                }
                strBody += "<tr><td width=100 style=font-size:9pt;text-align:center>" + strItemName + "</td><td width=50 style= font-size: 9pt; color=red;text-align:center>" + decItemNum.ToString() + "票</td><td style=font-size:9pt><img src=../images/1_11.jpg height=10 width=" + strPercent + "%>" + strPercent + "%</td><td></td></tr>\n";
                

            }
            strBody += "";
            lblLog.Text = strBody;
            
        }


        public void Get(string strContent, string strjj)
        {

            string[] arrContent = strContent.Split(',');
            string strBody = "";
            strBody = "<tr><td width=100 colspan=3 style=font-size:9pt;text-align:center>问题：" + strjj + "</td></tr>\n";
            foreach (string strContentIN in arrContent)
            {
                strBody += "<tr><td width=100 colspan=3 style=font-size:9pt;text-align:center>" + strContentIN + "</td></tr>\n";

            }

            lblLog.Text = strBody;
        }

        public decimal GetNumAll(string strNum)
        {
            decimal decNumAll = 0;
            string[] arrNum = strNum.Split('|');
            foreach (string strNumIN in arrNum)
            {
                decNumAll += Convert.ToInt32(strNumIN.Split(',')[0].ToString());//截取第零位
            }
            return decNumAll;
        }

        public decimal GetPercent(decimal decItem, decimal decNumAll) //百分比
        {
            if (decNumAll == 0)
            {
                decNumAll++;
            }
            decimal decPercent = decItem / decNumAll;
            return decPercent;
        }
        #endregion

        //清空
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string strID = Request.QueryString["ID"];
            string stryy = Request.QueryString["QuestType"];
            YXShop.BLL.Accessories.Question questBll = new YXShop.BLL.Accessories.Question();
            YXShop.Model.Accessories.Question model = questBll.GetModelById(Convert.ToInt32(strID));
            if (Request.QueryString["QuestType"] == "1" || Request.QueryString["QuestType"] == "2")
            {
                model.QuestionDiscription = " ";
            }
            else
            {

                string strwt = model.QuestionDiscription;
                string[] arrContent = strwt.Split('|');
                string str = "";
                foreach (string strContentIN in arrContent)
                {
                    string str1 = " " + strContentIN.Split(',')[0].ToString() + ",0";
                    if (str == "")
                    {
                        str = str1;
                    }
                    else
                    {
                        str = str + "|" + str1;
                    }

                }

                model.QuestionDiscription = str;

            }
            questBll.Update(model);
            HttpContext.Current.Response.Redirect("question_log.aspx?ID=" + strID + "&QuestType=" + stryy + "");
        }
    }
}
